package com.aop.annotation;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;

/**
 * @author xinwang
 * @date 2020/2/24 22:59
 **/
public class AnnotationTest {

    @AddAnnotation(userId = 1,userName = "张三",arrays = {"10","20"})
    public void add(){

    };

    public void del(){

    }
    public static void main(String[] args) throws Exception {
        //通过反射获取注解上的信息
        Class<?> forName = Class.forName("com.aop.annotation.AnnotationTest");
        //getDeclaredMethods 获取当前类的所有方法 不包含继承
        Method[] methods = forName.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
            //getAnnotatedReturnType 获取改方法上是否存在注解
            AddAnnotation annotate = method.getDeclaredAnnotation(AddAnnotation.class);
             //当前类方法上没有这个注解直接跳出循环
            if (annotate==null){
                System.out.println("没有该注解的方法:"+method.getName());
                continue;
            }
            //前类方法上有这个注解
            System.out.println("userId:"+annotate.userId());
            System.out.println("userName:"+annotate.userName());
            for (String array : annotate.arrays()) {
                System.out.println("array:"+array);
            }

        }
    }
}
